/*
 * @Author: your name
 * @Date: 2022-07-14 13:54:23
 * @LastEditTime: 2022-07-14 22:14:16
 * @LastEditors: NB-SZ-658
 * @Description: In User Settings Edit
 * @FilePath: \antd-demo-ts\src\store\reducers\unpadata.ts
 */
const inieState: any[] = [
    { id: '1', title: '全国全面建立林长制目标如期实现' },
    { id: '2', title: '美国总统拜登抵达以色列开启对中东的首访' },
    { id: '3', title: '农业农村部新闻办公室举行新闻发布会' }
]
import { debuglog } from 'util'
import { UPDATA_DEL, UPDATA_ADD, UPDATA_EDIT, UPDATA_SEARCH } from '../constants'
export default function updata(state = inieState, action: any) {
    switch (action.type) {
        case UPDATA_DEL: {
            let stateInit = state.filter((item) => item.id !== action.id)
            window.localStorage.setItem('listData', JSON.stringify(stateInit))
            return [...stateInit]
        }
        case UPDATA_ADD: {
            window.localStorage.setItem('listData', JSON.stringify([action.parms, ...state]))
            return [action.parms, ...state]
        }
        case UPDATA_EDIT: {
            let stateInit = state.map((item: any) => {
                let obj = item
                if (item.id === action.parms.id) {
                    obj = action.parms
                }
                return obj
            })
            window.localStorage.setItem('listData', JSON.stringify(stateInit))
            return [...stateInit]
        }
        case UPDATA_SEARCH: {
            let stateInit: any[] = []
            let listData = window.localStorage.getItem('listData') || ''
            if (action.parms) {
                try {
                    if (listData != 'null') {
                        stateInit = JSON.parse(listData)
                    }

                } catch { }
                return stateInit.filter((item: any) => item.title.indexOf(action.parms) !== -1)
            } else {
                if (listData != 'null') {
                    stateInit = JSON.parse(listData)
                }
                return [...stateInit]
            }
        }

        default: {
            return state
        }
    }
}